package io.renren.common.utils;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONArray;
import io.renren.common.exception.RRException;
import io.renren.modules.psorder.entity.PsOrderEntity;

import java.util.HashMap;
import java.util.Map;

public class QmSendPush {

	public static PushPayload buildPushObject_android_and_ios(String registrationId, String pushJson) {
		Map<String, String> extras = new HashMap<>();
		extras.put("pushJson", pushJson);
    	return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(registrationId))
				.setNotification(Notification.newBuilder()
								.setAlert("您的订单有最新状态，请查看")
						        .addPlatformNotification(AndroidNotification.newBuilder()
										.addExtras(extras).build())
								.addPlatformNotification(IosNotification.newBuilder()
										.addExtra("pushJson", pushJson).build())
								.build()
								)
				.setOptions(Options.newBuilder()
						.setApnsProduction(true)
						.build())
                .build();
    }

	public static PushPayload buildPushObject_android_and_iosReg(String username, String pushJson) {
		return PushPayload.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.alias(username))
				.setMessage(Message.newBuilder()
						.setMsgContent(pushJson)
						.build())
				.build();
	}


	
	public static void sendPushToDriver(String registrationId,PsOrderEntity psOrder,JSONArray destinationJsonArray) {
    	JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
    	String pushJson = QmPushMsgUtils.pushPsOrderToDriver("1001", "推送配送订单号给司机", psOrder,destinationJsonArray);
    	PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
    	try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} 
    }
	
	/**
	 * 告知用户配送单被接
	 * @param registrationId
	 * @param psOrder
	 */
	public static void sendOrderReceivedToUser(String registrationId, PsOrderEntity psOrder,JSONArray destinationJsonArray) throws Exception {
    	JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
    	String pushJson = QmPushMsgUtils.pushPsOrderToUser("1002", "反馈用户订单被接",psOrder,destinationJsonArray);
    	PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
//		PushResult result = jpushClient.sendPush(payload);
//		System.out.println("Got result - " + result);
		System.out.println("pushJson的大小 - " + pushJson.getBytes().length);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}
	
	/**
	 * 反馈用户开始出发
	 * @param registrationId
	 * @param psOrder
	 */
	public static void sendOrderStartedToUser(String registrationId, PsOrderEntity psOrder) throws Exception {
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1003", "反馈用户开始出发",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
		System.out.println("Got result - " + result);
//    	try {
//			PushResult result = jpushClient.sendPush(payload);
//			System.out.println("Got result - " + result);
//		} catch (APIConnectionException e) {
//			e.printStackTrace();
//		} catch (APIRequestException e) {
//			e.printStackTrace();
//		}
		
	}
	
//	/**
//	 * 推送给用户订单已完成了
//	 * @param registrationId
//	 * @param orderNum
//	 */
//	public static void sendOrderCompletedToPassenger(String registrationId,String orderNum){
//		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
//		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1004", "反馈用户订单已完成了",orderNum,"");
//		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
//    	try {
//			PushResult result = jpushClient.sendPush(payload);
//			System.out.println("Got result - " + result);
//		} catch (APIConnectionException e) {
//			e.printStackTrace();
//		} catch (APIRequestException e) {
//			e.printStackTrace();
//		}
//
//		return ;
//	}
//
	/**
	 * 推送给用户订单取消了
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendOrderCanceledToPassenger(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1005", "反馈用户订单已取消了",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);

	}

	/**
	 * 推送给司机订单已取消了
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendOrderCanceledToDriver(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToDriver("1006", "反馈司机订单已取消了",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
//		try {
//			PushResult result = jpushClient.sendPush(payload);
//			System.out.println("Got result - " + result);
//		} catch (APIConnectionException e) {
//			e.printStackTrace();
//		} catch (APIRequestException e) {
//			e.printStackTrace();
//		}

	}

	/**
	 * 推送给用户订单实体
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 * @throws Exception 向上抛给调用者异常
	 */
	public static void sendPsOrderToUserForPay(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1007", "让用户支付订单",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 推送给司机用户选择了现金支付
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendPsOrderCashPayToDriver(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToDriver("1008", "推送给司机用户选择了现金支付",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 反馈用户司机已到达发货点
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserArriveStart(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1009", "反馈用户司机已到达发货点",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 反馈用户司机已到目的地
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserArriveEnd(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1010", "反馈用户司机已到目的地",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 反馈用户订单已支付
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserPsOrderPaid(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1011", "反馈用户订单已支付",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 反馈用户订单目的地已修改
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserPsOrderEndModified(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1012", "反馈用户订单目的地已修改",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 反馈用户订单价格已修改
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserPsOrderPriceModified(String registrationId,PsOrderEntity psOrder) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1013", "反馈用户订单价格已修改",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		PushResult result = jpushClient.sendPush(payload);
	}

	/**
	 * 推送给司机订单已支付了
	 * @param registrationId 司机registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendAlreadyPayToDriver(String registrationId,PsOrderEntity psOrder){
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToDriver("1014", "推送给司机订单已支付了",psOrder,null);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		System.out.println("pushJson的大小 - " + pushJson.getBytes().length);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}

	/**
	 * 反馈用户已到达第几个目的地(多目的地情况)
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserPsOrderArriveMidDes(String registrationId,PsOrderEntity psOrder,Integer arriveDesNum,JSONArray destinationJsonArray) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1015", "反馈用户已到达第"+arriveDesNum+"个目的地",psOrder,destinationJsonArray);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
//		PushResult result = jpushClient.sendPush(payload);
		System.out.println("pushJson的大小 - " + pushJson.getBytes().length);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}

	/**
	 * 反馈用户已从第几个目的地卸货完成(多目的地情况)
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserCompletedUnloadToStart(String registrationId,PsOrderEntity psOrder,Integer arriveDesNum,JSONArray destinationJsonArray) throws Exception{
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1016", "反馈用户已从第"+arriveDesNum+"个目的地卸货完成",psOrder,destinationJsonArray);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
//		PushResult result = jpushClient.sendPush(payload);
		System.out.println("pushJson的大小 - " + pushJson.getBytes().length);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}

	/**
	 * 反馈用户订单暂不收费
	 * @param registrationId 用户registrationId
	 * @param psOrder 配送订单实体
	 */
	public static void sendToUserPsOrderZBSF(String registrationId,PsOrderEntity psOrder,JSONArray destinationJsonArray){
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToUser("1017", "反馈用户订单暂不收费",psOrder,destinationJsonArray);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		System.out.println("pushJson的大小 - " + pushJson.getBytes().length);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}

	public static void sendAppointmentOrderPushToDriver(String registrationId,PsOrderEntity psOrder,JSONArray destinationJsonArray) {
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushPsOrderToDriver("1018", "推送预约配送订单给司机", psOrder,destinationJsonArray);
		PushPayload payload = buildPushObject_android_and_ios(registrationId,pushJson);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		} catch (APIRequestException e) {
			e.printStackTrace();
			throw new RRException("极光推送发生异常",-2);
		}
	}



	public static void sendDriverinfoReg(String username,String content){
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushFastRegToDriver("2001","系统消息" ,content);
		PushPayload payload = buildPushObject_android_and_iosReg(username,pushJson);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
		return ;
	}
	public static void sendPassOverOrderGiveMil(String username,String content){
		JPushClient jpushClient = new JPushClient(QwjGlobal.JG_MASTER_SECRET, QwjGlobal.JG_APP_KEY);
		String pushJson = QmPushMsgUtils.pushFastRegToDriver("2002","系统消息" ,content);
		PushPayload payload = buildPushObject_android_and_iosReg(username,pushJson);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println("Got result - " + result);
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
		return ;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
